import {
    createEntityBase,
    loadAllBase,
    loadEntityBase,
    pageListBase,
    removeEntityBase,
    updateEntityBase
} from "../../../components/EntityApiBase.js";
import axios from "axios";
import {ElMessage} from "element-plus";

const basePath = import.meta.env.VITE_BACKEND_BASE_URL;

const entityPath = "/api/namespace";

async function loadEntity(id) {
    return await loadEntityBase(entityPath, id);
}

async function loadAll() {
    return await loadAllBase(entityPath);
}

async function pageList(paginationDTO) {
    return await pageListBase(entityPath, paginationDTO);
}

async function createEntity(entity) {
    return await createEntityBase(entityPath, entity);
}

async function updateEntity(id, entity) {
    return await updateEntityBase(entityPath, id, entity);
}

async function removeEntity(id) {
    return await removeEntityBase(entityPath, id);
}

async function loadNamespaceOptions() {
    try {
        let {data} = await axios(basePath + entityPath);
        if (data.code === '1') {
            let list = [];
            data.data.forEach(item => {
                list.push({
                    'value': item.name,
                    'label': item.description
                })
            })
            return list;
        } else {
            ElMessage.error(data.msg)
        }
    } catch (error) {
        console.log(error);
    }
}

export {loadEntity, loadAll, pageList, createEntity, updateEntity, removeEntity, loadNamespaceOptions};

